{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-30T03:07:04.254166Z",
     "start_time": "2025-05-30T03:07:04.233152Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# 生成模拟数据\n",
    "def generate_data(n_samples=300, n_features=2, n_clusters=4, random_state=42):\n",
    "    np.random.seed(random_state)\n",
    "    data = np.zeros((n_samples, n_features))\n",
    "    cluster_centers = np.array([[1, 1], [1, -1], [-1, 1], [-1, -1]])\n",
    "    for i in range(n_samples):\n",
    "        point = np.random.randn(n_features) + cluster_centers[i % n_clusters]\n",
    "        data[i, :] = point\n",
    "    return data\n",
    "\n",
    "# K-Means聚类算法\n",
    "def k_means(data, n_clusters, max_iter=100, tol=1e-4):\n",
    "    n_samples, n_features = data.shape\n",
    "\n",
    "    # 随机初始化簇中心\n",
    "    cluster_centers = data[np.random.choice(n_samples, n_clusters, replace=False)]\n",
    "\n",
    "    for _ in range(max_iter):\n",
    "        # 计算每个点到每个簇中心的距离\n",
    "        distances = np.sqrt(((data - cluster_centers[:, np.newaxis])**2).sum(axis=2))\n",
    "\n",
    "        # 找到每个点最近的簇中心\n",
    "        labels = np.argmin(distances, axis=0)\n",
    "\n",
    "        # 计算新的簇中心\n",
    "        new_cluster_centers = np.array([data[labels == i].mean(axis=0) for i in range(n_clusters)])\n",
    "\n",
    "        # 判断簇中心是否收敛\n",
    "        if np.all(np.abs(new_cluster_centers - cluster_centers) < tol):\n",
    "            break\n",
    "\n",
    "        cluster_centers = new_cluster_centers\n",
    "\n",
    "    return cluster_centers, labels\n",
    "\n",
    "# 主程序\n",
    "data = generate_data()\n",
    "n_clusters = 4\n",
    "cluster_centers, labels = k_means(data, n_clusters)\n",
    "\n",
    "# 打印结果\n",
    "print(\"Cluster Centers:\\n\", cluster_centers)\n",
    "print(\"Labels:\\n\", labels)\n"
   ],
   "id": "b5363f77ca338e91",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cluster Centers:\n",
      " [[-1.4947234  -1.46005837]\n",
      " [-1.18056063  0.59986689]\n",
      " [ 0.95164196  1.48721922]\n",
      " [ 1.24514656 -0.98189995]]\n",
      "Labels:\n",
      " [2 2 1 3 2 3 0 0 2 3 2 0 2 3 1 1 3 3 0 0 2 3 1 1 3 3 1 1 2 3 1 0 2 3 1 1 2\n",
      " 1 1 0 2 3 1 3 2 3 1 0 2 3 1 0 2 3 1 0 2 3 1 3 3 3 2 0 2 0 1 1 2 3 1 0 2 0\n",
      " 1 0 2 3 2 0 2 3 2 1 2 3 1 1 2 2 1 0 2 3 0 1 3 3 1 0 2 3 1 1 2 3 2 1 2 3 3\n",
      " 0 2 3 1 0 2 3 1 1 2 3 1 0 2 1 2 0 3 3 1 0 3 3 1 3 2 3 1 0 2 3 3 1 2 3 1 0\n",
      " 2 3 1 1 2 3 1 1 2 3 1 0 2 2 1 3 2 3 1 3 2 3 1 0 2 0 1 1 2 3 1 0 2 3 1 0 3\n",
      " 3 2 3 2 3 1 0 2 2 1 0 2 3 1 1 1 3 1 0 2 3 1 0 2 3 2 1 2 3 3 0 2 3 3 0 2 0\n",
      " 1 0 3 3 1 0 3 3 2 0 2 3 1 0 2 3 1 3 3 0 0 0 2 2 1 3 1 3 2 0 2 3 1 0 3 3 1\n",
      " 1 1 3 0 0 3 3 1 0 2 3 1 1 1 3 1 0 2 3 1 0 2 3 2 0 1 2 1 0 2 1 1 1 2 3 1 0\n",
      " 2 3 1 0]\n"
     ]
    }
   ],
   "execution_count": 5
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
